package com.flashcash.solu.utils;

import android.content.Context;
import android.content.Intent;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.x.leo.apphelper.log.XLog;

/**
 * Created by linchen on 16/03/2018.
 */

public class IntentUtils {
    public static <X> Intent intentWithJsonParameter(Context context, Class<X> classOfT, String parameterName, Object object) {
        Intent intent = new Intent(context, classOfT);
        Gson gson = new Gson();
        String json = gson.toJson(object);
        intent.putExtra(parameterName, json);

        return intent;
    }

    public static <X> X getJsonParameter(Intent intent, String parameterName, Class<X> classOfX) {
        String json = intent.getStringExtra(parameterName);
        if (json != null) {
            try {
                Gson gson = new Gson();
                return gson.fromJson(json, classOfX);
            } catch (JsonSyntaxException ex) {
                XLog.INSTANCE.e(parameterName, ex, 100);
            }
        }
        return null;
    }

}
